﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace StreetSmartAdmin
{
    public partial class Complain : System.Web.UI.Page
    {
        static String GridIndex ;

        public Userinfo user;
        protected void Page_Load(object sender, EventArgs e)
        {
            //((Label)Header.FindControl("lblUser")).Text = ((String)Session["UserName"]);
            //((Label)Header.FindControl("lblUser")).Text = ((Userinfo)Session["UserInfo"]).fullname; 
            //((Label)Header.FindControl("lblUser")).Text = (user.fullname);
            ADODotNetCRUD crud = new ADODotNetCRUD();
            user = crud.GetUser(User.Identity.Name);
            ((Label)Header.FindControl("lblUser")).Text = (user.fullname);
            //((Label)Header.FindControl("lblGroup")).Text = (user.groupname);
        }

        public void SqlDataSource2_Updated(object sender, SqlDataSourceStatusEventArgs e)
        {
            if (e.AffectedRows > 0)
            {
                //lblupdate.Text = "Last Successful Update at " + DateTime.Now.ToString(@"dd\/MM\/yyyy h\:mm:ss tt");

                string Username = user.fullname;
                string Behavior = "Update Record No." + GridIndex;

                string insertcommand = "insert into tblFeedback_Log (USERNAME,BEHAVIOR) values('" + Username + "','" + Behavior + "');";
                string connectionString = null;
                System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
                if (null != connString)
                    connectionString = connString.ConnectionString;

                System.Data.SqlClient.SqlConnection connection = null;
                connection = new System.Data.SqlClient.SqlConnection(connectionString);
                connection.Open();
                System.Data.SqlClient.SqlCommand command;
                command = connection.CreateCommand();
                command.CommandText = insertcommand;
                command.ExecuteNonQuery();
                connection.Close();
                GridView1.DataBind();
                GridView2.DataBind();
            }

        }





        private string UserCap(string username)
        {
            string str1 = username.Substring(0, 1);

            string str2 = username.Substring(1, username.Length - 1);
            return str1.ToUpper() + str2;
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            GridIndex = GridView1.SelectedDataKey.Value.ToString();
            lblupdate.Text = GridIndex;
        }

        protected void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
           
            int categoryID = (int)GridView1.DataKeys[e.RowIndex].Value;

            string Username = user.fullname;
            string Behavior = "Delete Record No." + categoryID;

            string insertcommand = "insert into tblFeedback_Log (USERNAME,BEHAVIOR) values('" + Username + "','" + Behavior + "');";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();

            GridView2.DataBind();

        }

        public void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {


            string Username = user.fullname;
            string Behavior = "Create New Record";

            string insertcommand = "insert into tblFeedback_Log (USERNAME,BEHAVIOR) values('" + Username + "','" + Behavior + "');";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();


            DetailsView1.ChangeMode(DetailsViewMode.Edit);
            GridView1.DataBind();
            GridView2.DataBind();

        }

        public void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {

            GridView1.DataBind();

        }






        protected void btnAddG1_click(object sender, EventArgs e)
        {
            DetailsView1.ChangeMode(DetailsViewMode.Insert);


        }

        public void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {

            DetailsView1.ChangeMode(DetailsViewMode.Edit);

            
            GridView1.DataBind();

            
           

        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            // Although I put only one http file control on the aspx page,
            // the following code can handle multiple file controls in a single aspx page.
            HttpFileCollection files = Request.Files;
            foreach (string fileTagName in files)
            {
                HttpPostedFile file = Request.Files[fileTagName];
                if (file.ContentLength > 0)
                {
                    // Due to the limit of the max for a int type, the largest file can be
                    // uploaded is 2147483647, which is very large anyway.
                    int size = file.ContentLength;
                    string name = file.FileName;
                    int position = name.LastIndexOf("\\");
                    name = name.Substring(position + 1);
                    string contentType = file.ContentType;
                    byte[] fileData = new byte[size];
                    file.InputStream.Read(fileData, 0, size);

                    //int CaseID = 2;
                    int CaseID = Int32.Parse(btn.CommandArgument.ToString());
                    //string CaseID = btn.CommandArgument.ToString();
                    //string CaseID = e.CommandArgument.ToString();

                    //FileUtilities.SaveFile(name, contentType, size, fileData);

                    using (SqlConnection connection = new SqlConnection())
                    {
                        string connectionString = null;
                        System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];

                        connectionString = connString.ConnectionString;
                        System.Data.SqlClient.SqlConnection connectionsql = null;

                        connectionsql = new System.Data.SqlClient.SqlConnection(connectionString);
                        connectionsql.Open();



                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = connectionsql;
                        cmd.CommandTimeout = 0;
                        //INSERT INTO [uploadtest] ([NAME], [SIZE], [DATA]) VALUES (@NAME, @SIZE, @DATA)
                        string commandText = "INSERT INTO tblFeedback_Case_File (CASEID, NAME, SIZE, DATA)VALUES(@CASEID, @NAME, @SIZE, @DATA)";

                        cmd.CommandText = commandText;
                        cmd.CommandType = CommandType.Text;

                        cmd.Parameters.Add("@CASEID", SqlDbType.Int);
                        cmd.Parameters.Add("@NAME", SqlDbType.NVarChar, 100);
                        //cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50);
                        cmd.Parameters.Add("@SIZE", SqlDbType.Int);
                        cmd.Parameters.Add("@DATA", SqlDbType.VarBinary);

                        cmd.Parameters["@CASEID"].Value = CaseID;
                        cmd.Parameters["@NAME"].Value = name;
                        //cmd.Parameters["@ContentType"].Value = contentType;
                        cmd.Parameters["@SIZE"].Value = size;
                        cmd.Parameters["@DATA"].Value = fileData;
                        cmd.ExecuteNonQuery();

                        connection.Close();
                    }
                }
                DetailsView1.DataBind();
                //GridView1.DataBind();
            }
        }

        public void SelID_TYPEChanged(object sender, EventArgs e)
        {
            if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals(""))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 0;
                
            }

            else if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("CI"))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 1;
            }

            else if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("SME"))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 2;
            }

            else if (((DropDownList)DetailsView1.FindControl("SelID_TYPE")).SelectedValue.Equals("RESI"))
            {
                ((MultiView)DetailsView1.FindControl("MultiView1")).ActiveViewIndex = 2;
            }

        }


        protected void AddNewCaseOnClick(object sender, EventArgs e)
        {


            string title = "CFID";
            string value = ((Label)DetailsView1.FindControl("lblCFID")).Text;

            string title2 = "LOG_BY";
            string value2 = user.fullname;

            insertRecord("tblFeedback_Case", title, value, title2, value2);
            ((DataList)DetailsView1.FindControl("DataList1")).DataBind();
        }

        protected void AddNewCommentOnClick(object sender, EventArgs e)
        {
            Button btncase = (Button)sender;
            string title = "CASEID";
            string caseid = btncase.CommandArgument.ToString();

            string title2 = "OPERATOR";
            string value2 = user.fullname;

            insertRecord("tblFeedback_Case_Comment", title, caseid, title2,value2);
            ((DataList)DetailsView1.FindControl("DataList1")).DataBind();
        }

        protected void insertRecord(string table, string ID, string value, string ID2, string value2)
        {
            string insertcommand = "insert into " + table + " (" + ID + "," + ID2 + ") values(" + value + ",'" + value2 + "');";
            string connectionString = null;
            System.Configuration.ConnectionStringSettings connString = System.Configuration.ConfigurationManager.ConnectionStrings["SQLServer2005DBConnectionString"];
            if (null != connString)
                connectionString = connString.ConnectionString;

            System.Data.SqlClient.SqlConnection connection = null;
            connection = new System.Data.SqlClient.SqlConnection(connectionString);
            connection.Open();
            System.Data.SqlClient.SqlCommand command;
            command = connection.CreateCommand();
            command.CommandText = insertcommand;
            command.ExecuteNonQuery();
            connection.Close();
        }

    }
}